package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.flight.domain.FlightOrderExceptionDetails;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.domain.FlightOrderException;
import com.ktgj.flight.service.IFlightOrderExceptionService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 机票订单异常Controller
 * 
 * @author ruoyi
 * @date 2022-06-21
 */
@RestController
@RequestMapping("/orderException")
public class FlightOrderExceptionController extends BaseController
{
    @Autowired
    private IFlightOrderExceptionService flightOrderExceptionService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询机票订单异常列表
     */
    //@PreAuthorize(hasPermi = "flight:exception:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody FlightOrderException flightOrderException)
    {
        startPage();
        List<FlightOrderException> list = flightOrderExceptionService.selectFlightOrderExceptionList(flightOrderException);
        return getDataTable(list);
    }

    /**
     * 导出机票订单异常列表
     */
    @PreAuthorize(hasPermi = "flight:exception:export")
    @Log(title = "机票订单异常", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightOrderException flightOrderException) throws IOException
    {
        List<FlightOrderException> list = flightOrderExceptionService.selectFlightOrderExceptionList(flightOrderException);
        ExcelUtil<FlightOrderException> util = new ExcelUtil<FlightOrderException>(FlightOrderException.class);
        util.exportExcel(response, list, "机票订单异常数据");
    }

    /**
     * 获取机票订单异常详细信息
     */
    @PreAuthorize(hasPermi = "flight:exception:query")
    @GetMapping(value = "/{exceptionId}")
    public AjaxResult getInfo(@PathVariable("exceptionId") Long exceptionId)
    {
        return AjaxResult.success(flightOrderExceptionService.selectFlightOrderExceptionById(exceptionId));
    }

    /**
     * 新增机票订单异常
     */
    @PreAuthorize(hasPermi = "flight:exception:add")
    @Log(title = "机票订单异常", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlightOrderException flightOrderException)
    {
        return toAjax(flightOrderExceptionService.insertFlightOrderException(flightOrderException));
    }

    /**
     * 修改机票订单异常
     */
    @PreAuthorize(hasPermi = "flight:exception:edit")
    @Log(title = "机票订单异常", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlightOrderException flightOrderException)
    {
        return toAjax(flightOrderExceptionService.updateFlightOrderException(flightOrderException));
    }

    /**
     * 删除机票订单异常
     */
    @PreAuthorize(hasPermi = "flight:exception:remove")
    @Log(title = "机票订单异常", businessType = BusinessType.DELETE)
	@DeleteMapping("/{exceptionIds}")
    public AjaxResult remove(@PathVariable Long[] exceptionIds)
    {
        return toAjax(flightOrderExceptionService.deleteFlightOrderExceptionByIds(exceptionIds));
    }

    /**
     * 机票订单异常列表
     * @param map
     * @return
     */
    @PostMapping("/flightOrderExceptionList")
    public TableDataInfo flightOrderExceptionList(@RequestBody Map<String, String> map) {
        startPage();
        String ticketChannel = map.get("ticketChannel");
        String ticketWay = map.get("ticketWay");
        String orderType = map.get("orderType");
        String orderId = map.get("orderId");
        List<FlightOrderException> exceptionList = flightOrderExceptionService.queryFlightOrderExceptionList(ticketChannel, ticketWay, orderType, orderId);
        return getDataTable(exceptionList);
    }

    /**
     * 出票处理-个人
     * @param map
     * @return
     */
    @PostMapping("/orderExceptionDetails")
    public AjaxResult orderExceptionDetails(@RequestBody Map<String, String> map) {
        String orderId = map.get("orderId");
        FlightOrderExceptionDetails exceptionDetails = flightOrderExceptionService.orderExceptionDetails(orderId);
        return AjaxResult.success(exceptionDetails);
    }

    /**
     * 操作PNR
     * @param map
     * @return
     */
    @PostMapping("/operationPNR")
    public AjaxResult operationPNR(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String token = request.getHeader("token");
        R<Employee> employeeByToken;
        try {
            employeeByToken = remoteCustomerService.getLoginEmployeeByToken(token);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (employeeByToken.getCode() == 200 && employeeByToken.getData() != null) {
            String orderId = map.get("orderId");
            return toAjax(flightOrderExceptionService.operationPNR(employeeByToken.getData(), orderId));
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }

    /**
     * 分配当前在线员工的异常订单任务
     * @return
     */
    @PostMapping("/assignEmployeeTask")
    public AjaxResult assignEmployeeTask()
    {
        boolean result = flightOrderExceptionService.assignEmployeeTask();
        if (result)
        {
            return AjaxResult.success("分配异常订单任务成功！");
        }else {
            return AjaxResult.error("分配异常订单任务失败！");
        }
    }
}
